//
//  HQUserInfoDetailViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/28.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQUserInfoDetailViewController.h"

@interface HQUserInfoDetailViewController ()
{
    NSString *tempProv_txt ;
    NSString *tempProv_id;
    NSArray *_provArray;
    NSArray *_cityArray;
}
@end

@implementation HQUserInfoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"信息设置";
    
    _city_id = @"0";
    _city_txt = @"";
    _prov_id = @"0";
    _prov_txt = @"";
    
    _isUploadUserIcon = NO;
    
    [self findMy];
}

/**
 设置头像
 
 @param sender 按钮
 */
- (IBAction)setIcon:(id)sender {
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
    UIAlertAction *takeAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeCamera context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
            if (isSuccess) {
                /** 上传图片*/
                _userIcon.image = image;
                _isUploadUserIcon = YES;
                [self editMy];
            } else {
                [self tostView:@"调取本地相册失败"];
            }
        }];
    }];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[ImagePickerTool sharedInstance] pickImageWithType:WLImagePickerTypeLocal context:self finishBlock:^(BOOL isSuccess, UIImage *image) {
            if (isSuccess) {
                /** 上传图片*/
                _userIcon.image = image;
                _isUploadUserIcon = YES;
                [self editMy];
            } else {
                [self tostView:@"调取本地相册失败"];
            }
        }];
    }];
    // 添加响应方式
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:takeAction];
    [actionSheetController addAction:photoAction];
    // 显示
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

/**
 设置厂名
 
 @param sender 按钮
 */
- (IBAction)setShopName:(id)sender {
    HQNameSettingViewController *nameSettingVc = [[HQNameSettingViewController alloc]init];
    nameSettingVc.pushType = @"shop";
    nameSettingVc.delegate = self;
    nameSettingVc.inputText = _shopName.text;
    [self.navigationController pushViewController:nameSettingVc animated:YES];
}

/**
 设置称呼
 
 @param sender 按钮
 */
- (IBAction)setUserName:(id)sender {
    HQNameSettingViewController *nameSettingVc = [[HQNameSettingViewController alloc]init];
    nameSettingVc.pushType = @"name";
    nameSettingVc.delegate = self;
    nameSettingVc.inputText = _realName.text;
    [self.navigationController pushViewController:nameSettingVc animated:YES];
}

/**
 设置地址
 
 @param sender 按钮
 */
- (IBAction)setAddress:(id)sender {
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getarea:@"0"] withMethod:[HQMethod getarea] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            _provArray = sucData[@"data"];
            NSMutableArray *m = [[NSMutableArray alloc]init];
            for(NSDictionary *dict in _provArray){
                [m addObject:[NSString stringWithFormat:@"%@",dict[@"haa_name"]]];
            }
            ZHPickView *zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
            [zhPickerView setPickeTitle:@"设置省份"];
            [zhPickerView selectObj:_prov_txt];
            zhPickerView.delegate = self;
            zhPickerView.tag = 2001;
            [zhPickerView show];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    if(pickView.tag == 2001){
        tempProv_txt = resultString;
        for(NSDictionary *dict in _provArray){
            if([dict[@"haa_name"] isEqualToString:resultString]){
                tempProv_id = dict[@"haa_id"];
                break;
            }
        }
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam getarea:tempProv_id] withMethod:[HQMethod getarea] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus] integerValue]== 1){
                _cityArray = sucData[@"data"];
                NSMutableArray *m = [[NSMutableArray alloc]init];
                for(NSDictionary *dict in _cityArray){
                    [m addObject:[NSString stringWithFormat:@"%@",dict[@"haa_name"]]];
                }
                ZHPickView *zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
                [zhPickerView setPickeTitle:@"设置城市"];
                zhPickerView.delegate = self;
                zhPickerView.tag = 2002;
                [zhPickerView show];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }else if(pickView.tag == 2002){
        _city_txt = resultString;
        for(NSDictionary *dict in _cityArray){
            if([dict[@"haa_name"] isEqualToString:resultString]){
                _city_id = dict[@"haa_id"];
                break;
            }
        }
        _prov_id = tempProv_id;
        _prov_txt = tempProv_txt;
        _address.text = [NSString stringWithFormat:@"%@ %@",_prov_txt,_city_txt];
        [self editMy];
    }
}

/**
 设置主要产品介绍
 
 @param sender 按钮
 */
- (IBAction)setMainProductIntroduce:(id)sender {
    HQNameSettingViewController *nameSettingVc = [[HQNameSettingViewController alloc]init];
    nameSettingVc.pushType = @"productIntroduce";
    nameSettingVc.delegate = self;
    nameSettingVc.inputText = _productInfo.text;
    [self.navigationController pushViewController:nameSettingVc animated:YES];
}


/**
 刷新界面数据
 
 @param pushType 修改类型
 @param text 文本
 */
-(void)reloadType:(NSString *)pushType withText:(NSString *)text{
    if([pushType isEqualToString:@"shop"]){
        _shopName.text = text;
    }else if([pushType isEqualToString:@"name"]){
        _realName.text = text;
    }else if([pushType isEqualToString:@"phone"]){
        _phone.text = text;
    }else if([pushType isEqualToString:@"productIntroduce"]){
        _productInfo.text = text;
    }
    [self editMy];
}

/**
 获取个人信息
 */
-(void)findMy{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam findMy:[UserModelManager userID]] withMethod:[HQMethod userfinMy] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _isUploadUserIcon = NO;
            [_userIcon setImageWithURL:[NSURL URLWithString:sucData[@"data"][@"portrait"]]];
            _realName.text = sucData[@"data"][@"real_name"];
            _shopName.text = sucData[@"data"][@"shop_name"];
            _phone.text = [UserModelManager phone];
            _city_id = [NSString stringWithFormat:@"%@",sucData[@"data"][@"city_id"]];
            _city_txt = sucData[@"data"][@"city_txt"];
            _prov_id = [NSString stringWithFormat:@"%@",sucData[@"data"][@"prov_id"]];
            _prov_txt = sucData[@"data"][@"prov_txt"];
            _address.text = [NSString stringWithFormat:@"%@ %@",_prov_txt,_city_txt];
            _productInfo.text = sucData[@"data"][@"range_desc"];
            
            UserModel *userModel = [[UserModel alloc]init];
            userModel.username = sucData[@"data"][@"real_name"];
            userModel.shopName = sucData[@"data"][@"shop_name"];
            userModel.userId = [UserModelManager userID];
            userModel.phone = [UserModelManager phone];
            userModel.portrait = sucData[@"data"][@"portrait"];
            userModel.position = [UserModelManager position]; 
            [UserModelManager saveUserModel:userModel withLoginState:YES];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 编辑个人信息
 */
-(void)editMy{
    if(_isUploadUserIcon){
        [self showHudText:@"上传图片..."];
        HQNetWork *hqnetwork = [[HQNetWork alloc]init];
        [hqnetwork httpUploadImages:_userIcon.image URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
            [self hiddnHud];
            if([suc[kStatus]integerValue] == 1){
                _imgUrl = [[suc objectForKey:@"data"] objectForKey:@"up_imgurl"];
                [self showHudText:@""];
                [HQNetWork requetParam:[HQParam editMy:[UserModelManager userID] withImg:_imgUrl withRealName:_realName.text withShop_name:_shopName.text prov_id:_prov_id prov_txt:_prov_txt city_id:_city_id city_txt:_city_txt range_desc:_productInfo.text] withMethod:[HQMethod userEditMy] suc:^(NSDictionary *sucData) {
                    [self hiddnHud];
                    if([sucData[kStatus]integerValue] == 1){
                        [self tostView:@"个人信息修改成功"];
                        [self findMy];
                    }else{
                        [self tostView:sucData[kInfo]];
                    }
                } fail:^{
                    [self hiddenErrorHud];
                }];
            }else{
                [self tostView:suc[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }else{
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam editMy:[UserModelManager userID] withImg:@"" withRealName:_realName.text withShop_name:_shopName.text prov_id:_prov_id prov_txt:_prov_txt city_id:_city_id city_txt:_city_txt range_desc:_productInfo.text] withMethod:[HQMethod userEditMy] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                [self tostView:@"个人信息修改成功"];
                [self findMy];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }
}
@end
